package com.example.ywj.toutiao.utils;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.ywj.toutiao.R;
import com.example.ywj.toutiao.bean.News;
import com.example.ywj.toutiao.bean.User;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.List;

import es.dmoral.toasty.Toasty;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MySaveListBaseAdapter extends BaseAdapter {
    private static final String TAG = "MyAttentionBaseAdapter";
    private List<News> list;
    public Boolean flag = false;
    //String username;
    public MySaveListBaseAdapter(List<News> list){
        this.list = list;
        //this.username = SpUtils.getString(this,"username");
    }
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //int currentType = getItemViewType(position);
        ViewHolder viewHolder ;
        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.savelist_item,null);
            viewHolder = new ViewHolder();
            viewHolder.avatar = convertView.findViewById(R.id.save_list_avatar);
            viewHolder.nickname = convertView.findViewById(R.id.save_list_nickname);
            viewHolder.time = convertView.findViewById(R.id.save_list_time);
            viewHolder.title = convertView.findViewById(R.id.save_list_title);
            viewHolder.conrent_pic = convertView.findViewById(R.id.save_list_pic);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.placeholder).error(R.drawable.error_img);
        String url = Constant.BASEURL+"public/images/"+list.get(position).getAvatar()+".jpg";
        Glide.with(parent.getContext()).load(url).apply(requestOptions).into(viewHolder.avatar);
        viewHolder.nickname.setText(list.get(position).getAuthor());
        viewHolder.time.setText(list.get(position).getTime());
        viewHolder.title.setText(list.get(position).getTitle());
        String arr[] = list.get(position).getImages().split("&");
        String url1 = Constant.BASEURL+"public/images/"+arr[0]+".jpg";
        //Log.i(TAG, "getView: "+url1);
        Glide.with(parent.getContext()).load(url1).apply(requestOptions).into(viewHolder.conrent_pic);
        return convertView;
    }
    class ViewHolder{
        ImageView avatar;
        TextView nickname;
        TextView time;
        TextView title;
        ImageView conrent_pic;
    }
}
